//
//  CourseRecommendViewController.m
//  ClassRoom
//
//  Created by apple on 12-11-30.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CourseRecommendViewController.h"
#import "CourseRecommendTableViewCell.h"
#import "CourseInformationViewController.h"
#import "ASIHTTPRequest.h"
#import "UrlData.h"
#import "JSONKit.h"

@interface CourseRecommendViewController ()

@end

@implementation CourseRecommendViewController
@synthesize tableView = _tableView;
@synthesize waitIndicatorView;
@synthesize loadinglabel;
@synthesize resultArray;
//@synthesize courseInformationViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self requestData];
    
    
    [self displayActiveIndicatorView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark UIActivityIndicatorView

//开始显示 UIActivityIndicatorView
- (void) displayActiveIndicatorView 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    if (waitIndicatorView == nil) {
        waitIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 100, 46, 46)];
        loadinglabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 140, 80, 30)];
        loadinglabel.text=@"loading...";
        loadinglabel.BackgroundColor=[UIColor clearColor];
        [waitIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [waitIndicatorView startAnimating];
        
        [self.view addSubview:waitIndicatorView];
        [self.view addSubview:loadinglabel];
    }
    
    [pool release];
}
//结束显示 UIActivityIndicatorView
- (void) dismissActiveIndicatorView
{
    if (waitIndicatorView)
    {
        [waitIndicatorView stopAnimating];
        [waitIndicatorView removeFromSuperview];
        [waitIndicatorView release]; waitIndicatorView = nil;
        [loadinglabel removeFromSuperview];
        [loadinglabel release];
        loadinglabel=nil;
    }
}


#pragma mark -
#pragma mark ASIHTTPRequest
-(void)requestData
{
    NSString *urlString=[NSString stringWithFormat:@"%@%@",ClassRoom_URL,SearchCourseSummary_URL];
    
    NSLog(@"urlstring=%@",urlString);
    NSURL *url=[NSURL URLWithString:urlString];
    NSLog(@"url=%@",url);
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDelegate:self];
    [request startAsynchronous];
    
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"requestFinished");
    NSString *responseString = [request responseString];
    NSLog(@"responseString------>>%@",responseString);
    
    NSMutableDictionary *result=[[responseString objectFromJSONString] retain];
    NSString *errer = [result objectForKey:@"error"];
    NSLog(@"%@",errer);
    NSLog(@"result = %@",result);
    [self loginDataOpinion:errer result:result];
    [self dismissActiveIndicatorView];//结束显示ActiveIndicatorView
    [result release];
    
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
    
}

- (void)loginDataOpinion:(NSString *)error result:(NSMutableDictionary *)result{
    if ([error intValue] == 0) {
        
        NSLog(@"%@",result);
        NSMutableDictionary *searchCourseSummaryDic=(NSMutableDictionary *)[result objectForKey:@"searchCourseSummary"];
        NSLog(@"searchCourseSummaryDic = %@",searchCourseSummaryDic);
        resultArray = [[searchCourseSummaryDic  objectForKey:@"course"] retain];
        NSLog(@"resultArray = %d",[resultArray count]);
        
        [self setTableView];
    }
}

#pragma mark -
#pragma mark UITableView

- (void)setTableView{
    _tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480 - 20 - 44) style:UITableViewStylePlain] autorelease];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [resultArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    CourseRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[[CourseRecommendTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    //设置tablecell的内容
    [cell setCellData:(NSDictionary *)[resultArray objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *largeImageURL = [NSString stringWithFormat:@"http://www.sinaimg.cn/dy/slidenews/3_img/2012_52/37294_245310_464968.jpg"];
    CourseInformationViewController *courseInformationViewController = [[[CourseInformationViewController alloc] init] autorelease];
    courseInformationViewController.imageURL = [NSURL URLWithString:largeImageURL];
    [self.navigationController pushViewController:courseInformationViewController animated:YES];
    [courseInformationViewController SetSomeChanges:(NSDictionary *)[[resultArray objectAtIndex:indexPath.row] objectForKey:@"information"]buttonEnable:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


@end
